package com.zbkj.crmeb.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.crmeb.store.dao.StoreProductRewardDao;
import com.zbkj.crmeb.store.model.StoreProductReward;
import com.zbkj.crmeb.store.request.StoreProductRewardSearchRequest;
import com.zbkj.crmeb.store.response.StoreProductRewardResponse;
import com.zbkj.crmeb.store.service.StoreProductRewardService;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;

import com.zbkj.crmeb.user.model.UserLevelGood;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * +---------------------------------------------------------------------- | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +---------------------------------------------------------------------- | Copyright (c) 2016~2020 https://www.crmeb.com All rights
 * reserved. +---------------------------------------------------------------------- | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +---------------------------------------------------------------------- | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Service
public class StoreProductRewardServiceImpl extends ServiceImpl<StoreProductRewardDao, StoreProductReward>
    implements StoreProductRewardService {

    @Resource
    private StoreProductRewardDao dao;

    @Override
    public PageInfo<StoreProductRewardResponse> getList(StoreProductRewardSearchRequest request, PageParamRequest pageParamRequest) {
        Page<StoreProductReward> storeProductPage = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

        //带 StoreProduct 类的多条件查询
        LambdaQueryWrapper<StoreProductReward> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //关键字搜索
        if (!StringUtils.isBlank(request.getKeywords())) {
            lambdaQueryWrapper.and(i -> i
                .or().eq(StoreProductReward::getId, request.getKeywords())
                .or().like(StoreProductReward::getGoodName, request.getKeywords())
                .or().like(StoreProductReward::getGoodId, request.getKeywords()));
        }
        lambdaQueryWrapper.orderByDesc(StoreProductReward::getId);
        List<StoreProductReward> storeProducts = dao.selectList(lambdaQueryWrapper);
        List<StoreProductRewardResponse> storeProductResponses = new ArrayList<>();
        for (StoreProductReward product : storeProducts) {
            StoreProductRewardResponse rewardResponse = new StoreProductRewardResponse();
            BeanUtils.copyProperties(product, rewardResponse);
            storeProductResponses.add(rewardResponse);
        }
        return CommonPage.copyPageInfo(storeProductPage, storeProductResponses);
    }

    @Override
    public StoreProductReward getByGoodIdNeId(Integer goodId, Integer id) {
        LambdaQueryWrapper<StoreProductReward> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (id != null) {
            lambdaQueryWrapper.ne(StoreProductReward::getId, id);
        }
        if (goodId != null) {
            lambdaQueryWrapper.eq(StoreProductReward::getGoodId, goodId);
        }
        return dao.selectOne(lambdaQueryWrapper);
    }

    @Override
    public StoreProductReward getByGoodId(Integer goodId) {
        LambdaQueryWrapper<StoreProductReward> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (goodId != null) {
            lambdaQueryWrapper.eq(StoreProductReward::getGoodId, goodId);
        }
        return dao.selectOne(lambdaQueryWrapper);
    }
}

